package com.example.canteen.controller;

import com.example.canteen.Response;
import com.example.canteen.dto.UserDTO;
import com.example.canteen.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @PostMapping("/name")
    public Response<UserDTO> getUserByName(@RequestBody UserDTO userDTO) {
        return Response.newSuccess(userService.getUserByName(userDTO.getUsername()));
    }

    @PostMapping("/all")
    public Response<Set<UserDTO>> getAllUsers() {
        return Response.newSuccess(userService.getAllUsers());
    }

    @PostMapping("/login")
    public Response<Map<String, Object>> login(@RequestBody UserDTO userDTO) {
        return Response.newSuccess(userService.login(userDTO.getUsername(), userDTO.getPassword()));
    }

    @PostMapping("/register")
    public Response<Long> register(@RequestBody UserDTO userDTO) {
        return Response.newSuccess(userService.register(userDTO));
    }

    @PostMapping("/update")
    public Response<UserDTO> updateUserById(@RequestBody UserDTO userDTO) {
        return Response.newSuccess(userService.updateUserById(userDTO.getId(), userDTO.getUsername(), userDTO.getPassword(), userDTO.getType(), userDTO.getDepartment()));
    }

    @PostMapping("/delete")
    public void deleteUserById(@RequestBody UserDTO userDTO) {
        userService.deleteUserById(userDTO.getId());
    }
}
